package com.jlf.tool.toolkit;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 字符串的工具包
 *
 * @author wujr
 * 2023/7/7
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/7 1.0 新增]
 */
public class StringToolkit {
    private static final Pattern linePattern = Pattern.compile("_(\\w)");

    public static String camel2Underline(String camel){
        String[] str = StringUtils.splitByCharacterTypeCamelCase(camel);
        return Arrays.stream(str).map(String::toLowerCase).collect(Collectors.joining("_"));
    }
    /**
     * 下划线转驼峰
     * @param str 下划线字符串
     * @return 驼峰字符串
     */
    public static String underline2Camel(String str) {
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
